{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from glob import glob\n",
    "import tests\n",
    "import shutil\n",
    "from tests.tutorials import test_notebook, clear_outputs_in_dir\n",
    "from functools import partial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with tests.cd('../hardware/victims/firmware'):\n",
    "    dirs = glob('simpleserial-base-lab*')\n",
    "    dirs.extend(glob('glitch-simple-lab*'))\n",
    "    dirs.extend(glob('simpleserial-aes-lab*'))\n",
    "    for d in dirs:\n",
    "        shutil.rmtree(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "output_dir = os.path.abspath(os.path.join('..', 'tutorials'))\n",
    "test_notebook = partial(test_notebook, allow_errors=False, output_dir=output_dir)\n",
    "\n",
    "# make sure the images are copied over for linking\n",
    "try:\n",
    "    os.mkdir(os.path.join(output_dir, 'img'))\n",
    "except FileExistsError:\n",
    "    pass\n",
    "\n",
    "for image in glob('./img/*'):\n",
    "    _, image_name = os.path.split(image)\n",
    "    print(os.path.join(output_dir, 'img', image_name))\n",
    "    shutil.copyfile(image, os.path.join(output_dir, 'img', image_name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "testscope = 'OPENADC'\n",
    "testplat = 'CWLITEARM'\n",
    "crypt = 'TINYAES128C'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "testscope = 'OPENADC'\n",
    "testplat = 'CWLITEXMEGA'\n",
    "crypt = 'AVRCRYPTOLIB'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "testscope = 'CWNANO'\n",
    "testplat = 'CWNANO'\n",
    "crypt = 'TINYAES128C'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('PA_Intro_1-Firmware_Build_Setup.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('PA_Intro_2-Instruction_Differences.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('PA_Intro_3-Measuring_SNR_of_Target.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('PA_SPA_1-Timing_Analysis_with_Power_for_Password_Bypass.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('PA_DPA_1-Hamming_Weight_Measurement.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, CRYPTO_TARGET=crypt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('PA_DPA_2-Large_HW_Swings.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, CRYPTO_TARGET=crypt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#assert testplat != \"CWLITEXMEGA\" and testplat != \"XMEGA typically fails to break ~1 byte here and takes a while to run.\".format(testplat)\n",
    "test_notebook('PA_DPA_3-AES_DPA_Attack.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, CRYPTO_TARGET=crypt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('PA_CPA_1-Using_CW-Analyzer_for_CPA_Attack.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, num_traces=100, CRYPTO_TARGET=crypt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('PA_CPA_2-Manual_CPA_Attack.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, num_traces=100, CRYPTO_TARGET=crypt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('PA_CPA_3-Resynchronizing_Data_Traces.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, num_traces=200, CRYPTO_TARGET=crypt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert testplat != \"CWLITEXMEGA\" and testplat != \"CW303\", \"Platform {} is not 32bit!\".format(testplat)\n",
    "test_notebook('PA_CPA_5-32bit_AES.ipynb', SCOPETYPE=testscope, PLATFORM=testplat) #MAY FAIL, takes a long time so may want to skip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert testscope != \"CWNANO\", \"Tutorial not yet available for CWNANO\"\n",
    "test_notebook('PA_Multi_1-Breaking_AES-256_Bootloader.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('PA_TVLA_1-Performing_TVLA_Testing_for_Crypto_Validation.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('PA_Profiling_1-Template_Attacks_HW_Assumption.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert testscope != \"CWNANO\", \"Clock glitching not available on CWNANO\"\n",
    "test_notebook('Fault_1-Introduction_to_Clock_Glitch_Attacks.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, sample_size=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert testplat != \"CWLITEXMEGA\" and testplat != \"CW303\", \"Voltage glitching works poorly on platform {}\".format(testplat)\n",
    "#assert testscope != \"CWNANO\", \"Tutorial not yet available for CWNANO\"\n",
    "test_notebook('Fault_2-Introduction_to_Vcc_Glitch_Attacks.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert testscope != \"CWNANO\", \"Tutorial not yet available for CWNANO\"\n",
    "test_notebook('Fault_3-Glitch_Buffer_Attacks.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert testscope != \"CWNANO\", \"Tutorial not yet available for CWNANO\"\n",
    "test_notebook('Fault_4-AES_Differential_Fault_Analysis_Attacks.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, CRYPTO_TARGET=crypt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert testplat != \"CWLITEXMEGA\" and testplat != \"CW303\" and testplat != \"CWNANO\", \"RSA fault attack not available on platform {}\".format(testplat)\n",
    "test_notebook('Fault_5-RSA_Fault_Attack.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('!!Introduction_to_Jupyter!!.ipynb', SCOPETYPE=None, PLATFORM=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
